package it.lorenz.server.fk.business.ex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ValidationException extends AbstractBasicException {

    private static final long serialVersionUID = 3265597091750124784L;
    private List<String> messages;
    private Map<String, List<String>> fieldsMessages;

    private void init() {
        messages = new ArrayList<String>();
        fieldsMessages = new HashMap<String, List<String>>();
    }

    public ValidationException() {
        super();
        init();
    }

    public ValidationException(String message) {
        super(message);
        init();
    }

    public ValidationException(String message, Throwable cause) {
        super(message, cause);
        init();
    }

    public ValidationException(Throwable cause) {
        super(cause);
        init();
    }

    public void addMessage(String message) {
        messages.add(message);
    }

    public List<String> getMessages() {
        return messages;
    }

    public void addFieldMessage(String field, String message) {
        if (!fieldsMessages.containsKey(field)
           || fieldsMessages.get(field) == null) {
            fieldsMessages.put(field, new ArrayList<String>());
        }
        fieldsMessages.get(field).add(message);
    }

    public List<String> getFieldMessages(String field) {
        if (fieldsMessages.containsKey(field)) {
            return fieldsMessages.get(field);
        } else {
            return new ArrayList<String>();
        }
    }

    public Map<String, List<String>> getFieldsMessages() {
        return fieldsMessages;
    }

    public boolean haveMessages() {
        return getCause() != null || getMessage() != null
           || messages.size() > 0 || !fieldsMessages.isEmpty();
    }
}
